//
//  ConferenceInfoWS.m
//  SEIO2012
//
//  Created by Gonzalo Abella Dago on 31/05/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConferenceInfoWS.h"


@implementation ConferenceInfoWS

@synthesize principalURL;
@synthesize lastModifiedURL;
@synthesize conferenceInfo;

 -(void)dealloc {
	 [principalURL release];
	 [lastModifiedURL release];
	 [conferenceInfo release];
	 [super dealloc];
 }

- (id)initWithConferenceInfoItem:(ConferenceInfoDTO *)conferenceInfoItem{
    if (self = [super init]) {
		[self setConferenceInfo:conferenceInfoItem];
    }
    return self;
}

#pragma mark -
#pragma mark WebService Protocol

- (void)start {
	NSURL *url = [[NSURL alloc] initWithString:principalURL];
	NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
	[parser setDelegate:self];
	[parser parse];
	
	[url release];
	[parser setDelegate:nil];
	[parser release];
}

- (void)cancel {

}

- (BOOL)querying {
	return NO;
}

- (NSDate *)lastUpdate {
	return nil;
}

#pragma mark -
#pragma mark NSXMLParser Delegate Calls

- (void)parserDidStartDocument:(NSXMLParser *)parser {
	//NSLog(@"Document started");
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	NSLog(@"Document ended");
	NSMutableString *string = [[NSMutableString alloc] init];
	[string appendFormat:@"Abbreviation=%@\n", [conferenceInfo abbreviation]];
	[string appendFormat:@"cover_image count=%d",[[conferenceInfo cover_image] count]];
	NSLog(@"%@", string);
	
	[string release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
										namespaceURI:(NSString *)namespaceURI 
										qualifiedName:(NSString *)qualifiedName 
										attributes:(NSDictionary *)attributeDict {
	
	//NSLog(@"Element started=%@", elementName);
	if (![elementName isEqualToString:@"element"]) {
		currentElement = [NSString stringWithString:elementName];
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
									  namespaceURI:(NSString *)namespaceURI 
									 qualifiedName:(NSString *)qName {

	//NSLog(@"Element ended=%@", elementName);
	if (![elementName isEqualToString:@"element"]) {
		currentElement=nil;
	}									 
	
}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock {
	NSString *data = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
	//NSLog(@"CDATA founded: %@", data);
	
	if ( [currentElement isEqualToString:@"abbreviation"]) {
		NSLog(@"Funciona");
		[conferenceInfo setAbbreviation:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"title"]) {
		[conferenceInfo setTitle:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"title2"]) {
		[conferenceInfo setTitle2:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"logo_frontend"]) {
		[conferenceInfo setLogo_frontend:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"presentation"]) {
		[conferenceInfo setPresentation:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"home_text"]) {
		[conferenceInfo setHome_text:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"contact_text"]) {
		[conferenceInfo setContact_text:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"twitter_profile"]) {
		[conferenceInfo setTwitter_profile:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"linkedin_profile"]) {
		[conferenceInfo setLinkedin_profile:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"location_gmaps"]) {
		[conferenceInfo setLocation_gmaps:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"currency"]) {
		[conferenceInfo setCurrency:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"updated_at"]) {
		NSDate *date = [[NSDate alloc] initWithString:data];
		[conferenceInfo setUpdated_at:date];
		[date release];
		return;
	}
	
	if ( [currentElement isEqualToString:@"culture_default"]) {
		[conferenceInfo setCulture_default:data];
		return;
	}
	
	if ( [currentElement isEqualToString:@"conference_culture"]) {
		NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[conferenceInfo conference_culture]];
		[array addObject: data];
		[conferenceInfo setConference_culture:array];
		[array release];
		return;
	}
	
	if ( [currentElement isEqualToString:@"cover_image"]) {
		NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[conferenceInfo cover_image]];
		[array addObject: data];
		[conferenceInfo setCover_image:array];
		[array release];
		return;
	}
	 
	[data release];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	NSLog(@"Error occurred");
}

@end
